

#include "RSSCardStrategy.h"
#include "formatingClass.h"

#include <LDF/VisComponent.h>

#include <windows.h>
#include <GL/gl.h>
#include <stdio.h>
#include "glfont2.h"
#include "ImageLoading.h"


RSSCardStrategy::RSSCardStrategy(unsigned int *tex, std::string imageLocation, std::string textToDisplay) : VisComponentStrategy()
{
	myStringObject = new formatString(textToDisplay);
	fontInitialized = false;
	myTex = tex;

	imageObject = new ImageLoading();
	imageTex = imageObject->loadImageForTexture(imageLocation.c_str());
}

RSSCardStrategy::~RSSCardStrategy()
{
	if(myStringObject != NULL) delete myStringObject;
	if(imageObject != NULL) delete imageObject;
}


void RSSCardStrategy::displayCard()
{
	if(!fontInitialized)
	{	
		myStringObject->initialize("size18.glf", *myTex);
		fontInitialized = true;
	}
	
	// Checking if this component is selected
	//component->print();
	


	float w2 = component->getWidth() / 2.0f;
	float h2 = component->getHeight() / 2.0f;

	glColor3f(0.0f, 0.23f, 0.0f);
		glBegin(GL_QUADS);
			glVertex3f(-w2,-h2,0);
			glVertex3f(w2, -h2,0);
			glVertex3f(w2, h2, 0);
			glVertex3f(-w2, h2, 0);
		glEnd();


	//printf("w2: %f   h2: %f  \n", w2, h2);
	
	glEnable(GL_TEXTURE_2D);
		//this color for not interfering in the color of the image
		glColor3f(1.0, 1.0, 1.0);
		//Using the image texture 
		glBindTexture(GL_TEXTURE_2D, imageTex);
		glBegin(GL_QUADS);
			glTexCoord2f(0,0);
			glVertex3f(-w2+h2/10,-h2+h2/10,0);
			glTexCoord2f(1,0);
			glVertex3f(0,-h2+h2/10,0);
			glTexCoord2f(1,1);
			glVertex3f(0,h2-h2/10,0);
			glTexCoord2f(0,1);
			glVertex3f(-w2+h2/10, h2-h2/10,0);

		glEnd();
		glDisable(GL_TEXTURE_2D);

		
	//text in white
	glColor3f(1.0F, 1.0F, 1.0F);
	//enable again texture
	glEnable(GL_TEXTURE_2D);
	//Prepare everything to draw the text (glBindTexture is called in Begin Function) Bind a new texture
	myStringObject->getFontUse()->Begin();
	//Draw the whole Text
	myStringObject->drawTheWholeText(w2 - h2/5, 2*h2 - h2/5, w2/10, h2 - h2/10);
	//disable the texture
	glDisable(GL_TEXTURE_2D);	
	//show to the screen
	glFlush();
}

void RSSCardStrategy::draw(const std::vector<unsigned long>& selectedIds)
{
		displayCard();
}

void RSSCardStrategy::drawForPicking()
{
	float w2 = component->getWidth() / 2.0f;
	float h2 = component->getHeight() / 2.0f;

	// Starting at the lower left corner and going counter-clockwise
	float v0[3], v1[3], v2[3], v3[3];
	v0[0] = -w2; v0[1] = -h2; v0[2] = 0;
	v1[0] = w2; v1[1] = -h2; v1[2] = 0;
	v2[0] = w2; v2[1] = h2; v2[2] = 0;
	v3[0] = -w2; v3[1] = h2; v3[2] = 0;
	
	unsigned char pickingColor[3] = { 0, 0, 0 };
	component->getIdColor(pickingColor);
	glColor3ubv(pickingColor);
	glBegin(GL_QUAD_STRIP);
		glVertex3fv(v0);
		glVertex3fv(v1);
		glVertex3fv(v3);
		glVertex3fv(v2);
	glEnd();

}
